package com.gz.logistics.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.app.Define;
import com.gz.logistics.app.OutingAssembleDetailActivity;
import com.gz.logistics.library.pojo.OrderDetail;
import com.gz.logistics.library.pojo.OrderInfo;
import com.gz.logistics.library.pojo.OutingOrderDetail;
import com.gz.logistics.library.pojo.ProductInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 预出库单详情 界面适配器
 * author wanner1991
 */
public class OutingAssembleDetailAdapter extends BaseAdapter {
    private String TAG = OutingAssembleDetailAdapter.class.getSimpleName();
    private OnClickSignLogisticsBtnListener myCallBack;
    private List<OutingOrderDetail> mDataList;
    private Context mContext;

    public OutingAssembleDetailAdapter(Context mContext,List<OutingOrderDetail> mDataList,OnClickSignLogisticsBtnListener myCallBack) {
        this.mDataList = mDataList;
        this.mContext = mContext;
        this.myCallBack  = myCallBack;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {

        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.outing_orders_item,null);
            holder = new ViewHolder();
            holder.tv_order_number = (TextView) view.findViewById(R.id.tv_order_number);
            holder.tv_create_time = (TextView) view.findViewById(R.id.tv_create_time);
            holder.tv_receiver_name = (TextView) view.findViewById(R.id.tv_receiver_name);
            holder.tv_receiver_phone = (TextView) view.findViewById(R.id.tv_receiver_phone);
            holder.tv_receiver_address = (TextView) view.findViewById(R.id.tv_receiver_address);
            holder.tv_receiver_cpucbt = (TextView) view.findViewById(R.id.tv_receiver_cpucbt);
            holder.tv_pay_type = (TextView) view.findViewById(R.id.tv_pay_type);
            holder.tv_money = (TextView) view.findViewById(R.id.tv_money);
            holder.tv_notes = (TextView) view.findViewById(R.id.tv_notes);
            holder.tv_order_price = (TextView) view.findViewById(R.id.tv_order_price);
            holder.tv_logistic_status = (TextView) view.findViewById(R.id.tv_logistic_status);
            holder.btn_logistics_confirm = (Button) view.findViewById(R.id.btn_logistics_confirm);

            holder.list_products = (LinearLayout) view.findViewById(R.id.list_products);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        final  OutingOrderDetail info  = mDataList.get(i);
        holder.tv_order_number.setText("订单编号："+info.getSn());
        holder.tv_receiver_name.setText("收货人姓名："+info.getReceiver_name());
        holder.tv_receiver_phone.setText("收货人电话："+info.getReceiver_tel());
        holder.tv_receiver_address.setText("收货人地址："+info.getReceiver_add());

        if(info.getCpucbt().equals("0")){
            holder.tv_receiver_cpucbt.setText("提货方式："+"送货上门");
        }else{
            holder.tv_receiver_cpucbt.setText("提货方式："+"自提");
        }
        if (info.getPay_type().equals("0")){
            holder.tv_pay_type.setText("支付方式："+"在线支付");
        }else{
            holder.tv_pay_type.setText("支付方式："+"货到付款");
        }
        holder.tv_money.setText("应收金额：￥"+info.getMoney());
        holder.tv_notes.setText("备注："+info.getNotes());
        holder.tv_order_price.setText("￥"+info.getMoney());
        holder.tv_logistic_status.setText(info.getOstatus());
        if (OutingAssembleDetailActivity.status==1){
            holder.btn_logistics_confirm.setVisibility(View.INVISIBLE);
        }else if(OutingAssembleDetailActivity.status==2){
            if (info.getOstatus().equals("未签收")||info.getOstatus().equals("已发货")){
                holder.btn_logistics_confirm.setVisibility(View.VISIBLE);
            }else if(info.getOstatus().equals("用户撤单")||info.getOstatus().equals("订单失效")){
                holder.btn_logistics_confirm.setVisibility(View.GONE);
            }
        }
        holder.btn_logistics_confirm.setTag(info);
        holder.btn_logistics_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OutingOrderDetail clickOrder = (OutingOrderDetail) view.getTag();
                if (clickOrder != null){
                    myCallBack.onClickSignLogisticsBtn(clickOrder);
                }

            }
        });
        // 初始化商品条目
        List<ProductInfo> productList = info.getProductInfos();
        holder.list_products.removeAllViews();
        for (int j = 0 ; j < productList.size() ; j++){
            ProductInfo productInfo = productList.get(j);
            View productItemView = LayoutInflater.from(mContext).inflate(R.layout.home_orders_products_item, null);
            ((TextView)productItemView.findViewById(R.id.tv_product_name)).setText(productInfo.getProduct_name());
            ((TextView)productItemView.findViewById(R.id.tv_total_price)).setText(productInfo.getProduct_num()+" × "+productInfo.getProduct_price());

            holder.list_products.addView(productItemView);
        }

        return view;
    }

    private class ViewHolder{
        TextView tv_order_number;
        TextView tv_create_time;
        TextView tv_receiver_name;
        TextView tv_receiver_phone;
        TextView tv_receiver_address;
        TextView tv_receiver_cpucbt; //提货方式
        TextView tv_pay_type; // 应付金额
        TextView tv_money;// 应收金额
        TextView tv_notes;
        LinearLayout list_products;
        TextView tv_order_price;
        TextView tv_logistic_status;
        Button btn_logistics_confirm;
    }

    public  interface OnClickSignLogisticsBtnListener{
         void onClickSignLogisticsBtn(OutingOrderDetail clickOrder);
    }
}
